
package com.hlkj.pay.controller.order;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.LocalContext;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.common.IDownloadExcelService;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.LocalAdminUserRequest;
import com.hlkj.pay.dto.home.HomePageCharsStatisticsDto;
import com.hlkj.pay.dto.order.OrderStatisticsDto;
import com.hlkj.pay.dto.order.OrderStatisticsGroupDto;
import com.hlkj.pay.dto.order.OrderStatisticsQueryDto;
import com.hlkj.pay.dto.pay.PayChannelQueryDto;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.enums.PayOrderEnums;
import com.hlkj.pay.infrastructure.model.merchant.MerchantInfoDO;
import com.hlkj.pay.infrastructure.model.order.OrderStatisticsDO;
import com.hlkj.pay.infrastructure.model.pay.PayChannelDO;
import com.hlkj.pay.service.merchant.IMerchantService;
import com.hlkj.pay.service.order.IOrderStatisticsService;
import com.hlkj.pay.service.pay.IPayChannelService;
import com.hlkj.pay.task.MerchantPaymentTasks;
import com.hlkj.pay.task.PayOrderStatisticsTasks;
import com.hlkj.pay.util.DateUtils;
import com.hlkj.pay.vo.order.req.StatisticsOrderPageQueryReq;
import com.hlkj.pay.vo.order.req.StatisticsOrderReportQueryReq;
import com.hlkj.pay.vo.order.resp.OrderReportStatisticsResp;
import com.hlkj.pay.vo.order.resp.OrderStatisticsResp;
import com.hlkj.pay.vo.order.resp.export.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author HlpayTeam
 * @date 2024/11/11 11:48
 */
@Tag(name = "订单统计管理")
@RestController
@RequestMapping("/v1/order/statistics")
@RequiredArgsConstructor
@Slf4j
public class OrderStatisticsController extends BaseController {

    private final IOrderStatisticsService orderStatisticsService;

    private final IDownloadExcelService downloadExcelService;

    private final IMerchantService merchantService;

    private final PayOrderStatisticsTasks payOrderStatisticsTasks;

    private final MerchantPaymentTasks merchantPaymentTasks;

    private final IPayChannelService payChannelService;

    @Operation(summary = "统计数据重新执行 前端不用关注")
    @GetMapping("/processPaymentStatistics")
    CommonResult<Void> processPaymentStatistics(String startDate,String endDate) {
        merchantPaymentTasks.processStatistics(startDate,endDate);
        return CommonResult.success();
    }

    @Operation(summary = "交易统计")
    @GetMapping("/tran")
    CommonResult<PageResult<OrderStatisticsResp>> queryTranPage(StatisticsOrderPageQueryReq statisticsOrderPageQueryReq) {
        OrderStatisticsQueryDto orderStatisticsQueryDto = new OrderStatisticsQueryDto();
        processQueryDate(statisticsOrderPageQueryReq, orderStatisticsQueryDto);
        orderStatisticsQueryDto.setSortBy(statisticsOrderPageQueryReq.getSortBy());
        orderStatisticsQueryDto.setSort(statisticsOrderPageQueryReq.getSort());
        // 1:1天 2:一个月 默认为1
        if (statisticsOrderPageQueryReq.getStartTime() != null) {
            LocalDate startDay = LocalDate.parse(statisticsOrderPageQueryReq.getStartTime());
            //时间类型 1:1天 2:周 3:月 4:年
            switch (statisticsOrderPageQueryReq.getQueryType()) {
                case 1:
                    orderStatisticsQueryDto.setStartQueryTime(DateUtils.shanghaiStartDate(startDay.toString(), DatePattern.NORM_DATE_PATTERN));
                    orderStatisticsQueryDto.setEndQueryTime(DateUtils.shanghaiEndDate(startDay.toString(), DatePattern.NORM_DATE_PATTERN));
                    break;
                case 2:
                    //获取当前周
                    LocalDate endDayOfWeek = startDay.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
                    LocalDate firstDayOfWeek = startDay.plusWeeks(-statisticsOrderPageQueryReq.getIntervalNum()).with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
                    orderStatisticsQueryDto.setStartQueryTime(DateUtils.shanghaiStartDate(firstDayOfWeek.toString(), DatePattern.NORM_DATE_PATTERN));
                    orderStatisticsQueryDto.setEndQueryTime(DateUtils.shanghaiEndDate(endDayOfWeek.toString(), DatePattern.NORM_DATE_PATTERN));
                    break;
                case 3:
                    LocalDate lastDayOfMonth = startDay.with(TemporalAdjusters.lastDayOfMonth());
                    LocalDate firstDayOfMonth = startDay.plusMonths(-statisticsOrderPageQueryReq.getIntervalNum()).with(TemporalAdjusters.firstDayOfMonth());
                    orderStatisticsQueryDto.setStartQueryTime(DateUtils.shanghaiStartDate(firstDayOfMonth.toString(), DatePattern.NORM_DATE_PATTERN));
                    orderStatisticsQueryDto.setEndQueryTime(DateUtils.shanghaiEndDate(lastDayOfMonth.toString(), DatePattern.NORM_DATE_PATTERN));
                    break;
                case 4:
                    LocalDate lastDayOfYear = startDay.with(TemporalAdjusters.lastDayOfYear());
                    LocalDate firstDayOfYear = startDay.with(TemporalAdjusters.firstDayOfYear());
                    orderStatisticsQueryDto.setStartQueryTime(DateUtils.shanghaiStartDate(firstDayOfYear.toString(), DatePattern.NORM_DATE_PATTERN));
                    orderStatisticsQueryDto.setEndQueryTime(DateUtils.shanghaiEndDate(lastDayOfYear.toString(), DatePattern.NORM_DATE_PATTERN));
                    break;
            }
        }

        orderStatisticsQueryDto.setQueryType(statisticsOrderPageQueryReq.getQueryType());
        orderStatisticsQueryDto.setSn(statisticsOrderPageQueryReq.getSn());

        if (statisticsOrderPageQueryReq.getExport() == 1) {
            String fileName = statisticsOrderPageQueryReq.getExportName();
            if (!StringUtils.hasText(fileName)) fileName = "交易统计导出_" + DateUtils.getDateString(DatePattern.PURE_DATETIME_PATTERN);
            downloadExcelService.createExcel(fileName, orderStatisticsQueryDto, StatisticsOrderFileExport.class);
            return CommonResult.success();
        }

        PageResult<OrderStatisticsGroupDto> page = orderStatisticsService.queryTranPage(orderStatisticsQueryDto);
        List<OrderStatisticsGroupDto> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }

        List<OrderStatisticsResp> orderStatisticsResps = cn.hutool.core.bean.BeanUtil.copyToList(list, OrderStatisticsResp.class);
        List<String> snList = list.stream().map(roleResp -> roleResp.getRelationId()).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(snList)) {
            List<MerchantInfoDO> merchantInfoList = merchantService.queryList(snList);
            Map<String, MerchantInfoDO> merchantInfoMap = merchantInfoList.stream()
                    .collect(Collectors.toMap(merchantInfoDO -> merchantInfoDO.getSn(), Function.identity(), (t1, t2) -> t1));
            orderStatisticsResps.stream().forEach(roleResp -> {
                roleResp.setSn(roleResp.getRelationId());
                MerchantInfoDO merchantInfoDO = merchantInfoMap.get(roleResp.getRelationId());
                if (merchantInfoDO != null) {
                    roleResp.setMerchantName(merchantInfoDO.getName());
                }
                roleResp.setTimeDesc(roleResp.getStatisticsTypeValue());

            });
        }
        return buildPageResult(page, orderStatisticsResps);
    }

    @Operation(summary = "商户统计")
    @GetMapping("/merchant")
    CommonResult<PageResult<OrderStatisticsResp>> queryMerchantPage(StatisticsOrderPageQueryReq statisticsOrderPageQueryReq) {
        OrderStatisticsQueryDto orderStatisticsQueryDto = new OrderStatisticsQueryDto();
        processQueryDate(statisticsOrderPageQueryReq, orderStatisticsQueryDto);
        orderStatisticsQueryDto.setRelationId(statisticsOrderPageQueryReq.getSn());
        orderStatisticsQueryDto.setType(PayOrderEnums.ORDER_STATISTICS_TYPE.SN.getCode());
        orderStatisticsQueryDto.setSortBy(statisticsOrderPageQueryReq.getSortBy());
        orderStatisticsQueryDto.setSort(statisticsOrderPageQueryReq.getSort());

        if (statisticsOrderPageQueryReq.getExport() == 1) {
            String fileName = statisticsOrderPageQueryReq.getExportName();
            if (!StringUtils.hasText(fileName)) fileName = "商户统计导出_" + DateUtils.getDateString(DatePattern.PURE_DATETIME_PATTERN);
            downloadExcelService.createExcel(fileName, orderStatisticsQueryDto, StatisticsMechantFileExport.class);
            return CommonResult.success();
        }

        PageResult<OrderStatisticsDO> page = orderStatisticsService.queryCommonPage(orderStatisticsQueryDto);
        List<OrderStatisticsDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        List<OrderStatisticsResp> orderStatisticsResps = cn.hutool.core.bean.BeanUtil.copyToList(list, OrderStatisticsResp.class);
        List<String> snList = list.stream().map(roleResp -> roleResp.getRelationId()).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(snList)) {
            List<MerchantInfoDO> merchantInfoList = merchantService.queryList(snList);
            Map<String, MerchantInfoDO> merchantInfoMap = merchantInfoList.stream()
                    .collect(Collectors.toMap(merchantInfoDO -> merchantInfoDO.getSn(), Function.identity(), (t1, t2) -> t1));
            orderStatisticsResps.stream().forEach(roleResp -> {
                roleResp.setSn(roleResp.getRelationId());
                MerchantInfoDO merchantInfoDO = merchantInfoMap.get(roleResp.getRelationId());
                if (merchantInfoDO != null) {
                    roleResp.setMerchantName(merchantInfoDO.getName());
                }
            });
        }
        return buildPageResult(page, orderStatisticsResps);
    }

    @Operation(summary = "代理商统计")
    @GetMapping("/agent")
    CommonResult<PageResult<OrderStatisticsResp>> queryAgentPage(StatisticsOrderPageQueryReq statisticsOrderPageQueryReq) {
        OrderStatisticsQueryDto orderStatisticsQueryDto = new OrderStatisticsQueryDto();
        processQueryDate(statisticsOrderPageQueryReq, orderStatisticsQueryDto);
        orderStatisticsQueryDto.setRelationId(statisticsOrderPageQueryReq.getAgentId());
        orderStatisticsQueryDto.setType(PayOrderEnums.ORDER_STATISTICS_TYPE.SN.getCode());
        orderStatisticsQueryDto.setSortBy(statisticsOrderPageQueryReq.getSortBy());
        orderStatisticsQueryDto.setSort(statisticsOrderPageQueryReq.getSort());

        if (statisticsOrderPageQueryReq.getExport() == 1) {
            String fileName = statisticsOrderPageQueryReq.getExportName();
            if (!StringUtils.hasText(fileName)) fileName = "代理商统计导出_" + DateUtils.getDateString(DatePattern.PURE_DATETIME_PATTERN);
            downloadExcelService.createExcel(fileName, orderStatisticsQueryDto, StatisticsAgentFileExport.class);
            return CommonResult.success();
        }

        PageResult<OrderStatisticsDO> page = orderStatisticsService.queryAgentPage(orderStatisticsQueryDto);
        List<OrderStatisticsDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        List<OrderStatisticsResp> orderStatisticsResps = cn.hutool.core.bean.BeanUtil.copyToList(list, OrderStatisticsResp.class);
        List<String> snList = list.stream().map(roleResp -> roleResp.getRelationId()).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(snList)) {
            List<MerchantInfoDO> merchantInfoList = merchantService.queryList(snList);
            Map<String, MerchantInfoDO> merchantInfoMap = merchantInfoList.stream()
                    .collect(Collectors.toMap(merchantInfoDO -> merchantInfoDO.getSn(), Function.identity(), (t1, t2) -> t1));
            orderStatisticsResps.stream().forEach(roleResp -> {
                roleResp.setAgentId(roleResp.getRelationId());
                MerchantInfoDO merchantInfoDO = merchantInfoMap.get(roleResp.getRelationId());
                if (merchantInfoDO != null) {
                    roleResp.setAgentName(merchantInfoDO.getName());
                }
            });
        }
        return buildPageResult(page, orderStatisticsResps);
    }

    @Operation(summary = "支付方式统计")
    @GetMapping("/payType")
    CommonResult<PageResult<OrderStatisticsResp>> queryPayTypePage(StatisticsOrderPageQueryReq statisticsOrderPageQueryReq) {
        OrderStatisticsQueryDto orderStatisticsQueryDto = new OrderStatisticsQueryDto();
        processQueryDate(statisticsOrderPageQueryReq, orderStatisticsQueryDto);
        orderStatisticsQueryDto.setRelationId(statisticsOrderPageQueryReq.getPayType());
        orderStatisticsQueryDto.setType(PayOrderEnums.ORDER_STATISTICS_TYPE.PAY_TYPE.getCode());
        orderStatisticsQueryDto.setSortBy(statisticsOrderPageQueryReq.getSortBy());
        orderStatisticsQueryDto.setSort(statisticsOrderPageQueryReq.getSort());

        if (statisticsOrderPageQueryReq.getExport() == 1) {
            String fileName = statisticsOrderPageQueryReq.getExportName();
            if (!StringUtils.hasText(fileName)) fileName = "支付方式统计导出_" + DateUtils.getDateString(DatePattern.PURE_DATETIME_PATTERN);
            downloadExcelService.createExcel(fileName, orderStatisticsQueryDto, StatisticsPayTypeFileExport.class);
            return CommonResult.success();
        }

        PageResult<OrderStatisticsDO> page = orderStatisticsService.queryCommonPage(orderStatisticsQueryDto);
        List<OrderStatisticsDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        List<OrderStatisticsResp> orderStatisticsResps = cn.hutool.core.bean.BeanUtil.copyToList(list, OrderStatisticsResp.class);
        orderStatisticsResps.stream().forEach(roleResp -> {
            roleResp.setPayType(roleResp.getRelationId());
        });
        return buildPageResult(page, orderStatisticsResps);
    }

    @Operation(summary = "应用统计")
    @GetMapping("/appId")
    CommonResult<PageResult<OrderStatisticsResp>> queryAppIdPage(StatisticsOrderPageQueryReq statisticsOrderPageQueryReq) {
        OrderStatisticsQueryDto orderStatisticsQueryDto = new OrderStatisticsQueryDto();
        processQueryDate(statisticsOrderPageQueryReq, orderStatisticsQueryDto);
        orderStatisticsQueryDto.setRelationId(statisticsOrderPageQueryReq.getAppId());
        orderStatisticsQueryDto.setType(PayOrderEnums.ORDER_STATISTICS_TYPE.APPID.getCode());
        orderStatisticsQueryDto.setSortBy(statisticsOrderPageQueryReq.getSortBy());
        orderStatisticsQueryDto.setSort(statisticsOrderPageQueryReq.getSort());

        if (statisticsOrderPageQueryReq.getExport() == 1) {
            String fileName = statisticsOrderPageQueryReq.getExportName();
            if (!StringUtils.hasText(fileName)) fileName = "应用统计导出_" + DateUtils.getDateString(DatePattern.PURE_DATETIME_PATTERN);
            downloadExcelService.createExcel(fileName, orderStatisticsQueryDto, StatisticsAppFileExport.class);
            return CommonResult.success();
        }

        PageResult<OrderStatisticsDO> page = orderStatisticsService.queryCommonPage(orderStatisticsQueryDto);
        List<OrderStatisticsDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        List<OrderStatisticsResp> orderStatisticsResps = cn.hutool.core.bean.BeanUtil.copyToList(list, OrderStatisticsResp.class);
        orderStatisticsResps.stream().forEach(roleResp -> {
            roleResp.setAppId(roleResp.getRelationId());
        });
        processMerchantList(orderStatisticsResps);
        return buildPageResult(page, orderStatisticsResps);
    }

    @Operation(summary = "应用通道统计")
    @GetMapping("/mchChannelCode")
    CommonResult<PageResult<OrderStatisticsResp>> queryMchChannelCodePage(StatisticsOrderPageQueryReq statisticsOrderPageQueryReq) {
        OrderStatisticsQueryDto orderStatisticsQueryDto = new OrderStatisticsQueryDto();
        processQueryDate(statisticsOrderPageQueryReq, orderStatisticsQueryDto);
        orderStatisticsQueryDto.setRelationId(statisticsOrderPageQueryReq.getMchChannelCode());
        orderStatisticsQueryDto.setSn(statisticsOrderPageQueryReq.getSn());
        orderStatisticsQueryDto.setType(PayOrderEnums.ORDER_STATISTICS_TYPE.MCH_CHANNEL_CODE.getCode());
        orderStatisticsQueryDto.setSubMchId(statisticsOrderPageQueryReq.getSubMchId());
        orderStatisticsQueryDto.setSortBy(statisticsOrderPageQueryReq.getSortBy());
        orderStatisticsQueryDto.setSort(statisticsOrderPageQueryReq.getSort());

        if (statisticsOrderPageQueryReq.getExport() == 1) {
            String fileName = statisticsOrderPageQueryReq.getExportName();
            if (!StringUtils.hasText(fileName)) fileName = "应用通道统计导出_" + DateUtils.getDateString(DatePattern.PURE_DATETIME_PATTERN);
            downloadExcelService.createExcel(fileName, orderStatisticsQueryDto, StatisticsChannelFileExport.class);
            return CommonResult.success();
        }

        PageResult<OrderStatisticsDto> page = orderStatisticsService.queryMchChannelCodePage(orderStatisticsQueryDto);
        List<OrderStatisticsDto> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        List<OrderStatisticsResp> orderStatisticsResps = cn.hutool.core.bean.BeanUtil.copyToList(list, OrderStatisticsResp.class);
        orderStatisticsResps.stream().forEach(roleResp -> {
            roleResp.setMchChannelCode(roleResp.getRelationId());
        });
        processMerchantList(orderStatisticsResps);
        return buildPageResult(page, orderStatisticsResps);
    }

    @Operation(summary = "统计数据重新执行 前端不用关注")
    @GetMapping("/processOrderStatistics")
    CommonResult<Void> processOrderStatistics(String startDate,String endDate) {
        payOrderStatisticsTasks.processOrderStatistics(startDate,endDate);
        return CommonResult.success();
    }

    @Operation(summary = "统计 日报，周报，月报，年报")
    @GetMapping("/queryStatistics")
    CommonResult<OrderReportStatisticsResp> queryStatistics(StatisticsOrderReportQueryReq statisticsOrderPageQueryReq) {
        OrderStatisticsQueryDto orderStatisticsQueryDto = new OrderStatisticsQueryDto();
        // 1:1天 2:一个月 默认为1
        if (statisticsOrderPageQueryReq.getStartTime() != null) {
            LocalDate startDay = LocalDate.parse(statisticsOrderPageQueryReq.getStartTime());
            //时间类型 1:1天 2:周 3:月 4:年
            switch (statisticsOrderPageQueryReq.getQueryType()) {
                case 1:
                    LocalDate endDay = startDay.plusDays(-statisticsOrderPageQueryReq.getIntervalNum());
                    orderStatisticsQueryDto.setStartQueryTime(DateUtils.shanghaiStartDate(endDay.toString(), DatePattern.NORM_DATE_PATTERN));
                    orderStatisticsQueryDto.setEndQueryTime(DateUtils.shanghaiEndDate(startDay.toString(), DatePattern.NORM_DATE_PATTERN));
                    break;
                case 2:
                    //获取当前周
                    LocalDate endDayOfWeek = startDay.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
                    LocalDate firstDayOfWeek = startDay.plusWeeks(-statisticsOrderPageQueryReq.getIntervalNum()).with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
                    orderStatisticsQueryDto.setStartQueryTime(DateUtils.shanghaiStartDate(firstDayOfWeek.toString(), DatePattern.NORM_DATE_PATTERN));
                    orderStatisticsQueryDto.setEndQueryTime(DateUtils.shanghaiEndDate(endDayOfWeek.toString(), DatePattern.NORM_DATE_PATTERN));
                    break;
                case 3:
                    LocalDate lastDayOfMonth = startDay.with(TemporalAdjusters.lastDayOfMonth());
                    LocalDate firstDayOfMonth = startDay.plusMonths(-statisticsOrderPageQueryReq.getIntervalNum()).with(TemporalAdjusters.firstDayOfMonth());
                    orderStatisticsQueryDto.setStartQueryTime(DateUtils.shanghaiStartDate(firstDayOfMonth.toString(), DatePattern.NORM_DATE_PATTERN));
                    orderStatisticsQueryDto.setEndQueryTime(DateUtils.shanghaiEndDate(lastDayOfMonth.toString(), DatePattern.NORM_DATE_PATTERN));
                    break;
                case 4:
                    LocalDate lastDayOfYear = startDay.with(TemporalAdjusters.lastDayOfYear());
                    LocalDate firstDayOfYear = startDay.with(TemporalAdjusters.firstDayOfYear());
                    orderStatisticsQueryDto.setStartQueryTime(DateUtils.shanghaiStartDate(firstDayOfYear.toString(), DatePattern.NORM_DATE_PATTERN));
                    orderStatisticsQueryDto.setEndQueryTime(DateUtils.shanghaiEndDate(lastDayOfYear.toString(), DatePattern.NORM_DATE_PATTERN));
                    break;
            }
        }
        //根据用户填充对应 店铺SN
        LocalAdminUserRequest localAdminUserRequest = (LocalAdminUserRequest) LocalContext.get();
        Integer sysType = localAdminUserRequest.getSysType();
        CommonEnum.SYS_TYPE sys_type = CommonEnum.SYS_TYPE.from(sysType);
        switch (sys_type) {
            case MERCHANT:
                orderStatisticsQueryDto.setSn(localAdminUserRequest.getSn());
                break;
            case AGENT:
                orderStatisticsQueryDto.setSnList(localAdminUserRequest.getQuerySnList());
                break;
        }

        orderStatisticsQueryDto.setQueryType(statisticsOrderPageQueryReq.getQueryType());
        List<OrderStatisticsGroupDto> list = orderStatisticsService.queryTranList(orderStatisticsQueryDto);
        OrderReportStatisticsResp reportStatisticsResp = new OrderReportStatisticsResp();
        if (!CollectionUtils.isEmpty(list)) {
            List<HomePageCharsStatisticsDto> orderStatisticsList = new ArrayList<>();
            List<String> keys = DateUtils.createDates(statisticsOrderPageQueryReq.getStartTime(), statisticsOrderPageQueryReq.getQueryType(), statisticsOrderPageQueryReq.getIntervalNum());
            String[] now = {statisticsOrderPageQueryReq.getStartTime()};
            switch (statisticsOrderPageQueryReq.getQueryType()) {
                case 2:
                    now[0] = DateUtils.formatWeek(statisticsOrderPageQueryReq.getStartTime());
                    break;
                case 3:
                    now[0] = DateUtils.formatDate(statisticsOrderPageQueryReq.getStartTime(), DatePattern.NORM_DATE_PATTERN, DatePattern.NORM_MONTH_PATTERN);
                    break;
                case 4://年份没有跨度，直接赋值
                    now[0] = LocalDate.parse(statisticsOrderPageQueryReq.getStartTime()).getYear() + "";
                    keys.add(now[0]);
                    break;
            }
            keys.forEach(k -> {
                OrderStatisticsGroupDto groupDto = list.stream().filter(obj -> obj.getStatisticsTypeValue().equals(k)).findFirst().orElse(null);
                HomePageCharsStatisticsDto homePageCharsStatisticsDto = new HomePageCharsStatisticsDto();
                if (groupDto != null) {
                    homePageCharsStatisticsDto.setSTime(groupDto.getStatisticsTypeValue());
                    homePageCharsStatisticsDto.setSumAmount(groupDto.getPayOrderAmount());
                    homePageCharsStatisticsDto.setSumCount(groupDto.getPayOrderCount());
                } else {
                    homePageCharsStatisticsDto.setSTime(k);
                }
                //取当前数据
                if (k.equals(now[0])) {
                    OrderStatisticsResp orderStatisticsResp = BeanUtil.copyProperties(groupDto, OrderStatisticsResp.class);
                    reportStatisticsResp.setOrderStatisticsResp(orderStatisticsResp);
                }
                orderStatisticsList.add(homePageCharsStatisticsDto);
            });
            reportStatisticsResp.setOrderStatisticsList(orderStatisticsList);
            orderStatisticsQueryDto.setType(2);//查询支付方式
            List<OrderStatisticsDO> payTypeList = orderStatisticsService.queryCommonList(orderStatisticsQueryDto);
            List<HomePageCharsStatisticsDto> pageCharsStatisticsDtos = new ArrayList<>();
            payTypeList.forEach(obj -> {
                HomePageCharsStatisticsDto homePageCharsStatisticsDto = new HomePageCharsStatisticsDto();
                homePageCharsStatisticsDto.setPayType(Integer.parseInt(obj.getRelationId()));
                homePageCharsStatisticsDto.setSumCount(obj.getPayOrderCount());
                homePageCharsStatisticsDto.setSumAmount(obj.getPayOrderAmount());
                pageCharsStatisticsDtos.add(homePageCharsStatisticsDto);
            });
            reportStatisticsResp.setPayTypeList(pageCharsStatisticsDtos);
        }
        return CommonResult.success(reportStatisticsResp);
    }

    @Operation(summary = "渠道统计")
    @GetMapping("/channelCode")
    CommonResult<PageResult<OrderStatisticsResp>> queryChannelCodePage(StatisticsOrderPageQueryReq statisticsOrderPageQueryReq) {
        OrderStatisticsQueryDto orderStatisticsQueryDto = new OrderStatisticsQueryDto();
        processQueryDate(statisticsOrderPageQueryReq, orderStatisticsQueryDto);
        orderStatisticsQueryDto.setRelationId(statisticsOrderPageQueryReq.getChannelCode());
        orderStatisticsQueryDto.setType(PayOrderEnums.ORDER_STATISTICS_TYPE.CHANNEL_CODE.getCode());
        orderStatisticsQueryDto.setSortBy(statisticsOrderPageQueryReq.getSortBy());
        orderStatisticsQueryDto.setSort(statisticsOrderPageQueryReq.getSort());

        if (statisticsOrderPageQueryReq.getExport() == 1) {
            String fileName = statisticsOrderPageQueryReq.getExportName();
            if (!StringUtils.hasText(fileName)) fileName = "支付渠道统计导出_" + DateUtils.getDateString(DatePattern.PURE_DATETIME_PATTERN);
            downloadExcelService.createExcel(fileName, orderStatisticsQueryDto, StatisticsChannelFileExport.class);
            return CommonResult.success();
        }

        PageResult<OrderStatisticsDto> page = orderStatisticsService.queryChannelCodePage(orderStatisticsQueryDto);
        List<OrderStatisticsDto> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }


        List<PayChannelDO> payChannelList = payChannelService.queryList(new PayChannelQueryDto());
        Map<String, PayChannelDO> payChannelMap = payChannelList.stream()
                .collect(Collectors.toMap(payChannel -> payChannel.getCode(), Function.identity(), (t1, t2) -> t1));

        List<OrderStatisticsResp> orderStatisticsResps = cn.hutool.core.bean.BeanUtil.copyToList(list, OrderStatisticsResp.class);
        orderStatisticsResps.stream().forEach(roleResp -> {
            roleResp.setChannelCode(roleResp.getRelationId());
            roleResp.setChannelCodeDesc(payChannelMap.get(roleResp.getChannelCode()).getName());
        });
        return buildPageResult(page, orderStatisticsResps);
    }

    @Operation(summary = "AUT统计")
    @GetMapping("/autCode")
    CommonResult<PageResult<OrderStatisticsResp>> queryAutCodePage(StatisticsOrderPageQueryReq statisticsOrderPageQueryReq) {
        OrderStatisticsQueryDto orderStatisticsQueryDto = new OrderStatisticsQueryDto();
        processQueryDate(statisticsOrderPageQueryReq, orderStatisticsQueryDto);
        orderStatisticsQueryDto.setRelationId(statisticsOrderPageQueryReq.getAutCode());
        orderStatisticsQueryDto.setType(PayOrderEnums.ORDER_STATISTICS_TYPE.AUT_CODE.getCode());
        orderStatisticsQueryDto.setSortBy(statisticsOrderPageQueryReq.getSortBy());
        orderStatisticsQueryDto.setSort(statisticsOrderPageQueryReq.getSort());

        if (statisticsOrderPageQueryReq.getExport() == 1) {
            String fileName = statisticsOrderPageQueryReq.getExportName();
            if (!StringUtils.hasText(fileName)) fileName = "AUT统计导出_" + DateUtils.getDateString(DatePattern.PURE_DATETIME_PATTERN);
            downloadExcelService.createExcel(fileName, orderStatisticsQueryDto, StatisticsPayAutCodeFileExport.class);
            return CommonResult.success();
        }

        PageResult<OrderStatisticsDto> page = orderStatisticsService.queryAutCodePage(orderStatisticsQueryDto);
        List<OrderStatisticsDto> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }

        List<OrderStatisticsResp> orderStatisticsResps = cn.hutool.core.bean.BeanUtil.copyToList(list, OrderStatisticsResp.class);

        List<PayChannelDO> payChannelList = payChannelService.queryList(new PayChannelQueryDto());
        Map<String, PayChannelDO> payChannelMap = payChannelList.stream()
                .collect(Collectors.toMap(payChannel -> payChannel.getCode(), Function.identity(), (t1, t2) -> t1));

        orderStatisticsResps.stream().forEach(roleResp -> {
            roleResp.setAutCode(roleResp.getRelationId());
            roleResp.setChannelCodeDesc(payChannelMap.get(roleResp.getChannelCode()).getName());
        });

        return buildPageResult(page, orderStatisticsResps);
    }
}
